<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>注册</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="./lib/layui-v2.6.3/css/layui.css" media="all">
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <style>
        body {background-image:url("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F0386a0c5861d8dca8012060c8b57fca.jpg&refer=http%3A%2F%2Fimg.zcool.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1657873228&t=381c10886210d3b31a83f3c9a5a9a89d");height:100%;width:100%;}
        #container{height:100%;width:100%;}
        input:-webkit-autofill {-webkit-box-shadow:inset 0 0 0 1000px #fff;background-color:transparent;}
        .admin-login-background {width:300px;height:300px;position:absolute;left:50%;top:40%;margin-left:-150px;margin-top:-100px;}
        .admin-header {text-align:center;margin-bottom:20px;color:#ffffff;font-weight:bold;font-size:28px}
        i {position:absolute;}
        .admin-login-background{
            background-color: rgba(0,0,0,0.2);
            padding: 20px;
            border-radius: 10%;
        }
        .layui-input{
            margin-top: 10px;
            border-radius: 10px;
            background-color: rgba(255,255,255,0.3);
            color: green;
        }
    </style>
</head>
<body>
<div id="container">
    <div></div>
    <div class="admin-login-background">
        <div class="admin-header">
            <span>注册账号</span>
        </div>
        <form class="layui-form" action="">
            <!--用户名-->
            <div>
                <input type="text" name="username" placeholder="请输入用户名" autocomplete="off" class="layui-input" value="">
            </div>
            <!--密码-->
            <div>
                <input type="password" name="password" placeholder="请输入密码" autocomplete="off" class="layui-input" value="">
            </div>
            <!--确认密码-->
            <div>
                <input type="password" name="repetition" placeholder="请确认密码" autocomplete="off" class="layui-input" value="">
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <input type="text" name="verity" required lay-verify="required" placeholder="验证码" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-inline" style="width: 50px;padding-top: 8px">
                    <img class="verifyImg" id="verifyImg" onclick="this.src='/captcha?num='+Math.random();" src="/captcha?num=1"  />
                </div>
            </div>
            <div align="center" style="margin-top: 15px">
                <!--注册按钮-->
                <button class="layui-btn" lay-submit="" lay-filter="register">确认注册</button>
                <!--注册按钮-->
                <button class="layui-btn" id="btn_login" type="button">返回登录</button>

            </div>

        </form>
    </div>
</div>
<script src="./lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;

        $("#btn_login").click(function () {
            location.href="/layuimini/login.html";
        });

        // 登录过期的时候，跳出ifram框架
        if (top.location !== self.location) top.location = self.location;

        // 进行登录操作
        form.on('submit(register)', function (data) {
            data = data.field;
            if (data.username === '') {
                layer.msg('用户名不能为空');
                return false;
            }
            if (data.password === '') {
                layer.msg('密码不能为空');
                return false;
            }
            if (data.password === '') {
                layer.msg('确认密码不能为空');
                return false;
            }
            if(data.password !== data.repetition) {
                layer.msg('两次输入的密码不一致');
                return false;
            }

            $.ajax({
                url: "/register",
                data: {
                    'username':data.username,
                    'password':data.password,
                    'verity':data.verity
                },
                type: 'post',
                datatype: 'json',
                success: function (res) {
                    // return: 0 用户名重复，1登录成功
                    switch (res) {
                        case -1:
                            layer.msg("验证码错误!",{time:1000});
                            break;
                        case 0:
                            layer.msg("该用户名已存在!",{time:1000});
                            break;
                        case 1: //登录成功
                            layer.msg("注册成功",{time:1000},function () {
                                // 注册成功后跳转到登录界面
                                location.href="/layuimini/login.html";
                            });
                            break;
                    }
                }
            });
            return false;
        });
    });
</script>
</body>
</html>